﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fcdbas.CodeBuilder.ety;
using Fcdbas.Core;
using DevExpress.XtraNavBar;

namespace Fcdbas.CodeBuilder
{
    public partial class NewLogicUnit : Form
    {
        string currModule = string.Empty;
        public NewLogicUnit(string _currModule)
        {
            InitializeComponent();
            currModule = _currModule;
        }

        public fnd_design_lu_tab newEtyLogicUnit = new fnd_design_lu_tab();

        private void txtName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnOK_Click(sender, e);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            fnd_design_lu_tab eyLuTab = new fnd_design_lu_tab();
            int iRst = 0;
            eyLuTab.module = currModule;
            eyLuTab.lu_name = txtName.Text;
            eyLuTab.package_name = eyLuTab.lu_name;//ConverString(eyLuTab.lu_name, "_") + "_Api";
            eyLuTab.view_name = (ConverString(eyLuTab.lu_name, "_")).ToUpper();
            eyLuTab.table_name = (ConverString(eyLuTab.lu_name, "_") + "_Tab").ToUpper();

            Dictionary<SalString, SalString> pars = new Dictionary<SalString, SalString>();
            //如果存在一条lu_name为空的则更新为现有的
            IList<fnd_design_lu_tab> ilEmpLu = Entity<ety.fnd_design_lu_tab>.FindAll("module='" + eyLuTab.module + "' AND lu_name ='' ", "", "", 0, 0);
            if (ilEmpLu.Count == 1)
            {
                pars.Add("@lu_name", eyLuTab.lu_name);
                pars.Add("@package_name", eyLuTab.package_name);
                pars.Add("@view_name", eyLuTab.view_name);
                pars.Add("@table_name", eyLuTab.table_name);
                pars.Add("@module", eyLuTab.module);
                iRst = eyLuTab.Execute("update fnd_design_lu_tab set lu_name=@lu_name,package_name=@package_name,view_name=@view_name,table_name=@table_name where module=@module", pars);
            }
            else
            {
                pars.Add("@lu_name", eyLuTab.lu_name);
                pars.Add("@package_name", eyLuTab.package_name);
                pars.Add("@view_name", eyLuTab.view_name);
                pars.Add("@table_name", eyLuTab.table_name);
                pars.Add("@module", eyLuTab.module);
                iRst = eyLuTab.Execute("insert into fnd_design_lu_tab (lu_name,package_name,view_name,table_name,module,rowversion,rowid) values(@lu_name,@package_name,@view_name,@table_name,@module,now(),uuid())", pars);
            }
            if (iRst == 0)
            {
                MessageBox.Show("保存失败。");
                return;
            }
            else
            {
                newEtyLogicUnit = eyLuTab;
                MessageBox.Show("保存成功。");
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        string ConverString(string str, string splitStr)
        {
            string _str = "";
            for (int i = 0; i < str.Length; i++)
            {
                if (System.Text.RegularExpressions.Regex.IsMatch(str[i].ToString(), "[A-Z]") && i != 0)
                {
                    _str = _str + splitStr;
                }
                _str = _str + str[i].ToString();
            }
            return _str;
        }
        
    }
}
